package com.heyu.cgsd.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * 小数点之后保留两位小数
     * @param string 源数据字符串
     * @return 保留后数据
     */
    public static String formatToSeparate2(String string) {
        DecimalFormat df = new DecimalFormat("#,##0.00");
        return df.format(new BigDecimal(string));
    }

    public static String formatToSeparate3(String string) {
        DecimalFormat df = new DecimalFormat("#,###.#########");
        return df.format(new BigDecimal(string));
    }

    public static String formatToSeparate4Exchange(String string) {
        DecimalFormat df = new DecimalFormat("#,###.####");
        return df.format(new BigDecimal(string));
    }

    public static String formatInteger(String string) {
        DecimalFormat df = new DecimalFormat();
        if (string.contains(".")) {
            String part1 = string.substring(0, string.lastIndexOf("."));
            String part2 = string.substring(string.lastIndexOf("."));
            return df.format(new BigDecimal(part1)) + part2;
        } else {
            return df.format(new BigDecimal(string));
        }
    }

    public static String replaceSeparate(String string) {
        return string.replaceAll(",", "");
    }

    public static String isChineseOrEnglish(String charaString) {
        boolean result;
        //英文
        result = charaString.matches("^[a-zA-Z]*");
        if (result) {
            return "isEnglish";
        }
        //中文
        String regEx = "[\\u4e00-\\u9fa5]+";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(charaString);
        result = m.find();
        if (result) {
            return "isChinese";
        } else {
            return "isChineseAndEnglish";//中英结合
        }
    }

    public static String checkChineseOrEnglish(String word) {
        byte[] bytes = word.getBytes();
        if (bytes.length == word.length()) {
            return "isEnglish";
        } else {
            return "isChinese";
        }
    }

    public static String sqliteEscape(String keyWord) {
        keyWord = keyWord.replace("/", "//");
        keyWord = keyWord.replace("'", "''");
        keyWord = keyWord.replace("[", "/[");
        keyWord = keyWord.replace("]", "/]");
        keyWord = keyWord.replace("%", "/%");
        keyWord = keyWord.replace("&", "/&");
        keyWord = keyWord.replace("_", "/_");
        keyWord = keyWord.replace("(", "/(");
        keyWord = keyWord.replace(")", "/)");
        return keyWord;
    }

    public static String bold2Red(String string) {
        string = string.replace("<b>", "<font color='#ff0000'>");
        string = string.replace("</b>", "</font>");
        return string;
    }
}
